package com.itheima.controller;

import com.itheima.advice.OneAnnotation;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        PageResult pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    @OneAnnotation
    @PostMapping
    public Result save(@RequestBody Emp emp) throws Exception {
        empService.saveEmp(emp);
        return Result.success();
    }

    @OneAnnotation
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        empService.deleteEmp(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Emp emp = empService.getEmpById(id);
        return Result.success(emp);
    }

    @OneAnnotation
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empService.updateEmp(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result GetEmp() {
        List<Emp> emp = empService.list();
        return Result.success(emp);
    }
}
